package com.chinasoft.web;

import com.alibaba.fastjson.JSON;
import com.chinasoft.config.SpringConfig;
import com.chinasoft.pojo.Brand;
import com.chinasoft.pojo.PageItem;
import com.chinasoft.service.BrandService;
import com.chinasoft.service.impl.BrandServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;

/**
 * @author 中软zhang
 * @create 2023-08-11 16:36
 */
@WebServlet("/brand/*")
//@Controller
public class BrandServlet extends BaseServlet {
    BrandService brandService;

    // 无参构造法
    public BrandServlet() {
        // ioc 对象
        ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfig.class);

        // 获取 brandService 实例化对象 bean
       this.brandService = ac.getBean(BrandService.class);
    }

    public void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 获取参数
        String sizeStr = req.getParameter("size");
        String currentStr = req.getParameter("current");
        // 转换为 Int 类型
        int size = Integer.parseInt(sizeStr);
        int current = Integer.parseInt(currentStr);
        // 获取 post请求体
        String queryJson = req.getReader().readLine();

        PageItem<Brand> pi = null;

        if (queryJson != null && queryJson.trim().length() > 0) {
            // 将json对象转换为 javaBean
            Brand brand = JSON.parseObject(queryJson, Brand.class);
            System.out.println("brand is " + brand);
            pi = brandService.queryConditionPage(brand, current, size);
        } else {
            // 获取列表对象
            pi = brandService.queryPage(current, size);
        }

        // 将对象转换为 json 字符串
        String json = JSON.toJSONString(pi);

        // 发送json串到前端
        resp.setContentType("text/json;charset=utf-8");
        resp.getWriter().write(json);
    }

    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取 前端发送的json串
        BufferedReader reader = req.getReader();
        String json = reader.readLine();
        System.out.println(json);

        // 将json串转换为 javaBean
        Brand brand = JSON.parseObject(json, Brand.class);
        System.out.println(brand);

        // 调用servcie 添加品牌数据
        boolean flag = brandService.insertBrand(brand);

        // 发送json串到前端
        resp.setContentType("text/json;charset=utf-8");
        if (flag) {
            resp.getWriter().write("succ");
        } else {
            resp.getWriter().write("fail");
        }
    }

    public void deleteSome(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取 前端发送的json串
        BufferedReader reader = req.getReader();
        String json = reader.readLine();
        // 将json串转换为 javaBean
        System.out.println(json);
        if (json != null && json.trim().length() != 0) {
            int[] ids = JSON.parseObject(json, int[].class);
            System.out.println(Arrays.toString(ids));

            // 调用servcie 添加品牌数据
            boolean flag = brandService.deleteSome(ids);

            // 发送json串到前端
            resp.setContentType("text/json;charset=utf-8");
            if (flag) {
                resp.getWriter().write("succ");
            } else {
                resp.getWriter().write("fail");
            }
        }

    }

}
